package com.taint.advice;


import com.taint.callgraph.CallRecorder;
import com.taint.logger.TraceEntry;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.implementation.bytecode.assign.Assigner;

import static com.taint.logger.TraceEndpoint.SINKENTRY;
import static com.taint.logger.TraceEndpoint.SINKEXIT;


public class SinkMethodAdvice {

    //static final Logger logger = LoggerFactory.getLogger(getClass());
    @Advice.OnMethodEnter
    public static TraceEntry MethodBefore(@Advice.Origin("#t") String type, @Advice.Origin("#m") String method, @Advice.Origin("#d") String desc, @Advice.AllArguments Object[] args) {

        //TaintLogger.info("sink enter",String.format("%s %s %s args: %s",type,method,sig, args!=null? Arrays.asList(args):null));
        //FlowTaintAgent.logger.info("sink  enter: "+String.format("%s %s %s args: %s",type,method,sig, args!=null?Arrays.asList(args):null));
        return CallRecorder.MethodBefore(SINKENTRY, type, method, desc, args);

    }


    @Advice.OnMethodExit
    public static void MethodAfter(@Advice.Enter TraceEntry item, @Advice.Origin("#t") String type, @Advice.Origin("#m") String method, @Advice.Origin("#s") String sig, @Advice.Return(typing = Assigner.Typing.DYNAMIC, readOnly = false) Object returned) {

        CallRecorder.MethodAfter(SINKEXIT, item, returned);
    }


}
